package com.example.student_blocket;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.EditText;
import android.widget.Toast;

public class Misc {

	public Misc() {}

	public Bitmap getImage(String uri) {
	    try {
	        URL url = new URL(uri);
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream inputStream = connection.getInputStream();
	        Bitmap image = BitmapFactory.decodeStream(inputStream);
	        return image;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	
	public boolean validateInput(String category) {
		if (category.length() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public void showErrorMessage(String message, Context context) {
		Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}
	
	public boolean validateFirstLastName(String names) {
		if (names.length() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean checkPasswords(EditText password, EditText rePassword) {
		if (password.getText().toString().equals(rePassword.getText().toString())) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean validatePassword(EditText password) {
		if (password.getText().toString().length() > 5) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean validateUsername(EditText username) {
		if (username.getText().toString().length() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean validateEmail(String email) {
		boolean valid = false;
		if (email.length() > 0) {
			if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
				valid = true;
			} else {
				valid = false;
			}
		} else {
			valid = false;
		}
		return valid;
	}
	
	public boolean validateAddress(EditText address) {
		if (address.getText().toString().length() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean validatePhoneNumber(EditText phone) {
		if (phone.getText().toString().length() > 0) {
			return true;
		} else {
			return false;
		}
	}
}
